exports.functions={
	form_loader: function(space,params) {
		if (params.name) {
			space.form=space.mfact.list("form",{name:params.name})[0];
		} else if (params.code) {
			space.form=space.mfact.list("form",{code:params.code})[0];
		}
		var config=space.form.config;
		space.form.config=null;
		eval("space.form.config="+config);
		function replacer(space,type,id,value) {
			if (typeof(value)!="string") return value;
			if (!value.match(/^{{/)) return value;
			return space.views.apply(value,space);
		}
		space.functions.recurse_object(space,space.form.config,replacer);
		for (var k in space.form) {
			space.form[k]=replacer(space,null,k,space.form[k]);
		}
		space.form_style=space.mfact.get("form_style",space.form.config.style_id);
	}
};

exports.controllers={
	form: function(space,params) {
		space.functions.form_loader(space,params);
		return space.controllers.form__view(space,params);
	},
	form_with_save: function(space,params) {
		space.functions.form_loader(space,params);
		var mode=space.fields.mode||"view";
		return space.controllers["form__"+mode](space,params);
	},
	form__view: function(space,params) {
		return space.output=space.views.process("widgets/"+(params.template||"forms/view"),space);
	},
	form__saved: function(space,params) {
		return space.output=space.views.process("widgets/"+(params.template||"forms/saved"),space);
	},
	form__save: function(space,params) {
		item=space.mfact.from_object(space.form.code,space.fields);
		if (params.email) {
			var ef=space.email_fields=[];
			for (var i=0;i<space.form.config.widgets.length;i++) {
				var w=space.form.config.widgets[i];
				switch (w.options.type) {
					case "label":
						break;
					case "textareaedit":
						ef.push({name: w.options.label, value: space.fields[w.options.name],line_break:1});
						break;
					case "textedit":
					case "date":
						ef.push({name: w.options.label, value: space.fields[w.options.name]});
						break;
					case "select":
						var id=space.fields[w.options.name];
						var value=id;
						for (var j=0;j<w.options.values.length;j++) if (w.options.values[j].id==id) value=w.options.values[j].name;
						ef.push({name: w.options.label, value: value});
						break;
					default:
						throw "Unknown widget type "+w.options.type;
				}
			}
			var htmlbody=space.views.process("widgets/"+(params.emailtemplate||"forms/email"),space);
			var to = params.email;
			//$headers ="From: =?utf-8?B?".base64_encode($mfrom_name) ."?= <$mfrom_mail>\n";
			var subject = "Form filled";
			var body = htmlbody.replace(/(<\/p>|<\/h\d>)/g,'\n').replace(/<.*?>/g,"");
			var headers = {
				From: params.from,
				"Content-Type": 'text/plain; charset="UTF-8"'/*,
				"MIME-Version":"1.0",
				"Content-Type":"multipart/mixed; boundary=\"frontier""*/
			};
/*			body+="\n--frontier\nContent-Type: text/html\n";
			body+=htmlbody;
			body+="\n--frontier\nContent-Type: text/html\n";*/
			var auth/* = {
				type: "login",
				user: "test",
				password: "secret"
			}*/;
			require("mail").mail(to, subject, body, headers, auth);
		}
		item.Save();
		return space.output=JSON.stringify({redirect:{url: space.action+"?mode=saved" }});
	}
};

